﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.IO;
using System.Threading;
using FileWatcher;
using EmailSendWizard;


namespace WindowsSevicerTest
{
    public class HKEXMonitor
    {
        public HKEXMonitor()
        {
            DataFileMonitor monitor = new DataFileMonitor(Properties.Settings.Default.BufferDirectory, "*.csv");
            Thread monitorThread = new Thread(new ThreadStart(monitor.watch));
            monitorThread.Start();
            DailyCaptureOn hsifCapture = new DailyCaptureOn("hsif");
            Thread hsif = new Thread(new ThreadStart(hsifCapture.threadStart));
            hsif.Start();
            Console.WriteLine("hsif start");
            DailyCaptureOn hsioCapture = new DailyCaptureOn("hsio");
            Thread hsio = new Thread(new ThreadStart(hsioCapture.threadStart));
            hsio.Start();
            Console.WriteLine("hsio start");
            DailyCaptureOn hhifCapture = new DailyCaptureOn("hhif");
            Thread hhif = new Thread(new ThreadStart(hhifCapture.threadStart));
            hhif.Start();
            Console.WriteLine("hhif start");
            DailyCaptureOn dqeCapture = new DailyCaptureOn("dqe");
            Thread dqe = new Thread(new ThreadStart(dqeCapture.threadStart));
            dqe.Start();
            Console.WriteLine("dqe start");
        }
        //Workflow: 
        //->DailyCaptureOn           <-------------------|                
        //   ->FileDownload(if exist)                    |
        //   ->UnzipAndDeleted(work with the zip file)   |
        //->DataFileMonitor(work with the csv file)      |
        //   ->InsertToDataBase(and delete the CSV file) |
        //   ->SendAnEmail                               |
        public class DailyCaptureOn
        {

            private string typeOfstock;

            public DailyCaptureOn(string typeOfstock)
            {
                // TODO: Complete member initialization
                this.typeOfstock = typeOfstock;
            }

            public void threadStart()
            {
                FileDownload fDownload = new FileDownload();
                DateTime dateTime = DateTime.Now;
                string dateyymmdd = dateTime.ToString("yyMMdd");
                //string url = "http://www.hkex.com.hk/eng/stat/dmstat/dayrpt/" + typeOfstock + "120323.zip";
                string url = "http://www.hkex.com.hk/eng/stat/dmstat/dayrpt/" + typeOfstock + dateyymmdd + ".zip";
                string result = fDownload.Download(Properties.Settings.Default.BufferDirectory, url);

                if (result == "downloaded success")
                {
                    Console.WriteLine(result + " " + typeOfstock + dateyymmdd + ".zip has downloaded");
                    StoreTheData(Properties.Settings.Default.BufferDirectory, Path.GetFileName(url));

                    DateTime tomorrow = new DateTime(DateTime.Today.AddDays(1).Ticks);
                    Console.WriteLine("Time Now : " + dateTime);

                    Console.WriteLine("Time Tomorrow: " + tomorrow);


                    TimeSpan datetimediff = tomorrow.Subtract(dateTime);
                    int timeWait = (int)Math.Ceiling(datetimediff.TotalMilliseconds);
                    Thread.Sleep(timeWait);
                    Console.WriteLine("Time until midnight: " + timeWait);

                }
                else
                {
                    Thread.Sleep(1000 * 3600);//Implement capture the HKEX daily report website each hour
                    //to get the daily report.
                    threadStart();
                }
            }

            private void StoreTheData(string dataDirectory, string fileName)
            {
                //Start to decompose the file
                UnzipAndDeleted unzipDelete = new UnzipAndDeleted();
                unzipDelete.unzipTo(dataDirectory + fileName, Properties.Settings.Default.BufferDirectory);
            }


        }
    }
}
